//
//  TTIMediaPlayerManager.m
//  chinaJuJiao
//
//  Created by hff on 15/6/18.
//  Copyright (c) 2015年 hfanfeng. All rights reserved.
//

#import "TTIMediaPlayerManager.h"
#import "AppMacro.h"
#import "AppDelegate.h"


#pragma mark - Singleton
static TTIMediaPlayerManager *g_instance = nil;

@interface TTIMediaPlayerManager ()<UIWebViewDelegate>

@property (strong, nonatomic) MPMoviePlayerViewController *playerView;
@property (nonatomic, strong) UIViewController *delegateViewController;
@property (nonatomic, strong) NSURL *videoUrl;

@end

@implementation TTIMediaPlayerManager

+(TTIMediaPlayerManager *)shareInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        g_instance = [[TTIMediaPlayerManager alloc] init];
        [g_instance initialize];
    });
    
    return g_instance; 
    
}

-(void)initialize
{
    self.playerView = [[MPMoviePlayerViewController alloc]  init];
    self.delegateViewController = [[UIViewController alloc] init];
    self.videoUrl = [[NSURL alloc] init];
}

-(void)dealloc{
    
    self.playerView = nil;

     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[self.playerView moviePlayer]];
}

#pragma mark - Init
- (void)mediaPlayerWithType:(id )delegate
                        url:(NSURL *)videoUrl
{
    //判断传过来的代理是否为控制器
    if (![delegate isKindOfClass:[UIViewController class]]) {
        NSAssert(![delegate isKindOfClass:[UIViewController class]], @"传入的Delegate不是ViewController");
        return;
    }

    
    self.delegateViewController = delegate;
    self.videoUrl = videoUrl;
    
    if (!self.playerView)
    {
        self.playerView = [[MPMoviePlayerViewController alloc]  init];
    }
    self.playerView.view.frame = [UIApplication sharedApplication].keyWindow.frame;//全屏播放

  

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:[self.playerView moviePlayer]];
    [self.playerView.moviePlayer setContentURL:self.videoUrl];

    
    self.playerView.moviePlayer.contentURL = self.videoUrl;
    self.playerView.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    self.playerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    self.playerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [self.playerView.moviePlayer setFullscreen:YES];
    [self.playerView.moviePlayer setShouldAutoplay:YES];
    [self.playerView.moviePlayer prepareToPlay];
   
    [self.delegateViewController presentViewController:self.playerView animated:YES completion:^{
        [self.playerView.moviePlayer play];
    }];
    

   //[self.delegateViewController presentViewController:self.playerView animated:YES completion:nil];
    
    //[self performSelectorOnMainThread:@selector(playVideo) withObject:nil waitUntilDone:YES];
    
  
    // [self.delegateViewController.view addSubview:self.playerView.view];
   // [self.delegateViewController.view bringSubviewToFront:self.playerView.view];
   // if (self.playerView.moviePlayer)
//    {
//        [self.playerView.moviePlayer play];
//
//    }
   
    //视频横屏播放
//    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//    appDelegate.isViedoFull = YES;

    
//    if (self.playerView.moviePlayer.fullscreen) {
//        
//        self.playerView.moviePlayer.fullscreen = NO;
//    }//内嵌式播放器全屏退出时 必须要设置 moviePlayer.fullscreen = NO；否则不退出！
//    
    
   // The movie formats supported on the iPhone are .mov, .m4v, mp4 or 3gp.
//    MPMoviePlayerController *player = [self.playerView moviePlayer];
//    player.controlStyle = MPMovieControlStyleFullscreen;
//    [player setFullscreen:YES animated:YES];
//   
//    [player prepareToPlay];
//    [player play];

}

-(void)playVideo
{
  
}


- (void) playVideoFinished:(NSNotification *)theNotification//当点击Done按键或者播放完毕时调用此函数
{
//    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//    appDelegate.isViedoFull = NO;
//    
 
    MPMoviePlayerController *player = [theNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [player stop];
    
    
    [self.playerView dismissViewControllerAnimated:YES completion:nil];
    [self.playerView.view removeFromSuperview];
    self.playerView = nil;
    
//    self.playerView = nil;
//    
//    [self.playerView dismissViewControllerAnimated:YES completion:^{
//        //[self.playerView.view removeFromSuperview];
//       // self.playerView = nil;
//
//    }];
  
    
    //消除警告
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
//                                (int64_t)(0.51 * NSEC_PER_SEC)),
//                   dispatch_get_main_queue(), ^{
//                       if (![self.playerView isBeingDismissed])
//                       {
//                           [self.playerView dismissViewControllerAnimated:YES completion:nil];
//                           
//                       }
//                   });
}

@end
